﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class SAPConfigDAL
    {
        /// <summary>
        /// 如果不存在则添加,存在则修改
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool Update(List<SAPConfigInfo> list)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                    foreach (SAPConfigInfo obj in list)
                    {
                        DbCommand dbCommand = DB.GetStoredProcCommand("SAPConfig_Update", obj.ConfigKey, obj.ConfigValue, obj.Remark);
                        DB.ExecuteScalar(dbCommand, trans);                        
                    }
                    trans.Commit();
                    return true;
                }
                catch (Exception exc)
                {
                    //发生异常，事务回滚
                    trans.Rollback();
                    return false;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }
        }

        public static IDataReader GetAll()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SAPConfig_GetAll");
            return DB.ExecuteReader(dbCommand);
        }

        
    }
}
